﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace psafth.RCTiming.Main.Pages.Dialogs
{
    /// <summary>
    /// Interaction logic for MakeRacesDialog.xaml
    /// </summary>
    public partial class MakeRacesDialog : Window
    {
        private Event _event;

        private MainWindow _mainWindow;

        public DBContext DbContext
        {
            get
            {
                return this._mainWindow.DbContext;
            }
            set
            {
                this._mainWindow.DbContext = value;
            }
        }


        private List<RaceClassGroup> _classGroups;

        public MakeRacesDialog(Event ev, MainWindow mw)
        {
            if (ev == null)
                return;

            this._classGroups = new List<RaceClassGroup>();
            this._mainWindow = mw;
            this._event = ev;
            InitializeComponent();

            foreach (IEnumerable<EventParticipant> group in ev.Participants.GroupBy(r => r.RaceClassId))
            {
                RaceClassGroup tmpGroup = new RaceClassGroup();

                tmpGroup.Competitors = group.ToList();
                if (tmpGroup.Competitors != null)
                    tmpGroup.RaceClass = tmpGroup.Competitors.FirstOrDefault().RaceClass;

                _classGroups.Add(tmpGroup);
            }

            this.rptClassGroups.ItemsSource = _classGroups;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            List<RaceClassGroup> tmpGroups = new List<RaceClassGroup>();

            rptClassGroups.Items.Cast<RaceClassGroup>().ToList().ForEach(item =>
            {
                var container = rptClassGroups.ItemContainerGenerator.ContainerFromItem(item);

                List<Control> children = AllChildren(container);

                CheckBox cbInclude = children.FirstOrDefault(c => c.Name == "cbInclude") as CheckBox;
                TextBox tbNumberOfHeats = children.FirstOrDefault(c => c.Name == "tbNumberOfHeats") as TextBox;
                TextBox tbLengthOfHeat = children.FirstOrDefault(c => c.Name == "tbLengthOfHeat") as TextBox;
                TextBox tbLengthOfFinal = children.FirstOrDefault(c => c.Name == "tbLengthOfFinal") as TextBox;

                if (cbInclude.IsChecked == true)
                {
                    item.NumberOfHeats = Convert.ToInt32(tbNumberOfHeats.Text);
                    item.HeatDuration = Convert.ToInt32(tbLengthOfHeat.Text);
                    item.FinalDuration = Convert.ToInt32(tbLengthOfFinal.Text);

                    tmpGroups.Add(item);
                }
            });

            HeatHelper.MakeHeats(ref _event, tmpGroups, true);



            DbContext.SaveChanges();

            this.Close();
        }

        // TODO: Implement this as extension method? At least move it
        public List<Control> AllChildren(DependencyObject parent)
        {
            List<Control> children = new List<Control>();

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);

                if (child is Control)
                    children.Add(child as Control);

                children.AddRange(AllChildren(child));
            }

            return children;
        }
    }
}
